package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.RecordBorrow;
import com.ruoyi.system.service.IRecordBorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 借阅管理Controller
 *
 * @author admin
 * @date 2022-03-10
 */
@RestController
@RequestMapping("/system/borrow")
public class RecordBorrowController extends BaseController {
    @Autowired
    private IRecordBorrowService recordBorrowService;

    /**
     * 查询借阅管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:borrow:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecordBorrow recordBorrow) {
        startPage();
        List<RecordBorrow> list = recordBorrowService.selectRecordBorrowList(recordBorrow);
        return getDataTable(list);
    }
    @GetMapping("/getcount")
    public AjaxResult getcount(RecordBorrow recordBorrow) {
        return AjaxResult.success(recordBorrowService.selectRecordBorrowGetCount(recordBorrow));
    }

    /**
     * 导出借阅管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:borrow:export')")
    @Log(title = "借阅管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecordBorrow recordBorrow) {
        List<RecordBorrow> list = recordBorrowService.selectRecordBorrowList(recordBorrow);
        ExcelUtil<RecordBorrow> util = new ExcelUtil<RecordBorrow>(RecordBorrow.class);
        util.exportExcel(response, list, "借阅管理数据");
    }

    /**
     * 获取借阅管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:borrow:query')")
    @GetMapping(value = "/{borrowId}")
    public AjaxResult getInfo(@PathVariable("borrowId") Long borrowId) {
        return AjaxResult.success(recordBorrowService.selectRecordBorrowByBorrowId(borrowId));
    }

    /**
     * 新增借阅管理
     */
    @PreAuthorize("@ss.hasPermi('system:borrow:add')")
    @Log(title = "借阅管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecordBorrow recordBorrow) {
        return toAjax(recordBorrowService.insertRecordBorrow(recordBorrow));
    }

    /**
     * 修改借阅管理
     */
    @PreAuthorize("@ss.hasPermi('system:borrow:edit')")
    @Log(title = "借阅管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecordBorrow recordBorrow) {
        return toAjax(recordBorrowService.updateRecordBorrow(recordBorrow));
    }

    /**
     * 删除借阅管理
     */
    @PreAuthorize("@ss.hasPermi('system:borrow:remove')")
    @Log(title = "借阅管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{borrowIds}")
    public AjaxResult remove(@PathVariable Long[] borrowIds) {
        return toAjax(recordBorrowService.deleteRecordBorrowByBorrowIds(borrowIds));
    }

    /**
     * 借阅审批通过
     */
    @PreAuthorize("@ss.hasPermi('system:borrow:approve')")
    @Log(title = "借阅审批通过", businessType = BusinessType.UPDATE)
    @PutMapping("/approve/{borrowIds}")
    public AjaxResult updateApprove(@PathVariable Long[] borrowIds) {
        return toAjax(recordBorrowService.updateApprove(borrowIds));
    }

    /**
     * 借阅审批拒绝
     */
    @PreAuthorize("@ss.hasPermi('system:borrow:reject')")
    @Log(title = "借阅审批拒绝", businessType = BusinessType.UPDATE)
    @PutMapping("/reject/{borrowIds}")
    public AjaxResult updateReject(@PathVariable Long[] borrowIds) {
        return toAjax(recordBorrowService.updateReject(borrowIds));
    }

    /**
     * 实体借阅归还
     */
    @PreAuthorize("@ss.hasPermi('system:borrow:giveback')")
    @Log(title = "实体借阅归还", businessType = BusinessType.UPDATE)
    @PutMapping("/giveback/{borrowIds}")
    public AjaxResult updateGiveBack(@PathVariable Long[] borrowIds) {
        return toAjax(recordBorrowService.updateGiveBack(borrowIds));
    }
}
